package com.fh.service.impl;

import com.fh.mapper.TCarMapper;
import com.fh.model.TCar;
import com.fh.pojo.CarDTO;
import com.fh.service.CarService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CarServiceImpl implements CarService {
    @Resource
    private TCarMapper tCarMapper;
    @Override
    public List addCar(Long userId, Long pid, Long count) {
        List<CarDTO> carDTOS= tCarMapper.queryCarListByUerId(userId);
            boolean flag=true;
            for (CarDTO cars :carDTOS){
                if(pid==cars.getPid()){
                    cars.setCount(count+cars.getCount());
                    flag=false;
                    tCarMapper.updateCar(cars);
                }
            }
            if(flag) {
                TCar tCar = new TCar();
                SimpleDateFormat sim = new SimpleDateFormat("yyyyMMddHHmmss");
                tCar.setCartId(Long.valueOf(sim.format(new Date())));
                tCar.setPcount(count);
                tCar.setPid(pid);
                tCar.setUserId(userId);
                tCarMapper.insert(tCar);
            }

        return carDTOS;
    }

    @Override
    public void deleteCar(Long pid,Long userId) {
        Map map = new HashMap();
        map.put("pid",pid);
        map.put("userId",userId);
        tCarMapper.deleteCarByPidAndUser(map);
    }

    @Override
    public List<CarDTO> queryCarListUserId(Long userId) {
        return tCarMapper.queryCarListByUerId(userId);
    }

}
